package com.shopcentre15.shopcentre.FormalShopExplot.Controller.Goods;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shopcentre15.shopcentre.FormalShopExplot.Entity.ShopGoods;
import com.shopcentre15.shopcentre.FormalShopExplot.Service.Goods.GoodsService;
import com.shopcentre15.shopcentre.FormalShopExplot.Utils.AliOSSUtils;
import com.shopcentre15.shopcentre.FormalShopExplot.Utils.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/goods_func")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private AliOSSUtils aliOSSUtils;
    @PostMapping("/up_goods")
    public String upGoods(@RequestParam Map<String,Object> goods, @RequestParam("file") MultipartFile file) throws IOException {

        try{
            String url = aliOSSUtils.upload(file);
            if(url == null){
                return "main";
            }
            goods.put("img",url);
            goods.put("state",0);
            if(goods.get("title") == null || goods.get("price") == null ){
                return "main";
            }
            if(goods.get("mid") ==null){
                return "login";
            }
            Integer addGoods = goodsService.addProduct(goods);
            if(addGoods > 0 ){
                return "main";
            }
            return "main";
        }catch (RuntimeException e){
            e.printStackTrace();
            return "main";
        }
    }
    //分页查询 limit方式 操作sql语句 切片
    @GetMapping("/getGoods")
    @ResponseBody
    public CommonResult getGoodsListByPage(@RequestParam int page, @RequestParam int size, @RequestParam int mid) {
        Map<String, Object> params = new HashMap<>();
        params.put("offset", page+1);
        params.put("limit", size);
        params.put("mid", mid);
        List<ShopGoods> goodsListByPage = goodsService.getGoodsListByPage(params);
        int total = goodsService.getTotalGoodsCount(mid);
        return CommonResult.success(goodsListByPage,total,null);
    }

    //pagehelper方式
    @GetMapping("/getGoodsPg")
    @ResponseBody
    public CommonResult getGoodsListByPageHelper(@RequestParam int page, @RequestParam int size, @RequestParam int mid) {
        // 设置分页参数
        PageHelper.startPage(page, size);
        List<ShopGoods> goodsListByPage = goodsService.getGoodsListByPageHelper(mid);
        PageInfo<ShopGoods> pageInfo = new PageInfo<>(goodsListByPage);
        return CommonResult.success(pageInfo.getList(), null ,pageInfo.getTotal());
    }

    @PostMapping("/updateGood")
    @ResponseBody
    public CommonResult updateGood(int id,int state){
        Integer updateProduct = goodsService.UpdateProduct(id,state);
        System.out.println(updateProduct);
        if(updateProduct > 0){
            return CommonResult.success();
        }
        return CommonResult.fail(500,"更新失败");
    }

    @GetMapping("/getGoodsByTitle")
    @ResponseBody
    public CommonResult getGoodsByTitle(@RequestParam("keyword") String Keyword,@RequestParam("mid") int mid){
        List<ShopGoods> goodsByTitle = goodsService.getGoodsByTitle(Keyword, mid);
        return CommonResult.success(goodsByTitle,null,null);
    }

    @PostMapping("/updateGoodInfo")
    @ResponseBody
    public CommonResult updateGoodInfo(@RequestParam Map<String,Object> params, @RequestParam("file") MultipartFile file) throws IOException {
        String url = aliOSSUtils.upload(file);
        if(url == null){
            return CommonResult.fail(500,"更新失败");
        }
        params.put("img",url);
        Integer updateGoodInfo = goodsService.updateGoodInfo(params);
        if(updateGoodInfo > 0){
            return CommonResult.success();
        }
        return CommonResult.fail(500,"更新失败");
    }

    @PostMapping("/updateBatch")
    @ResponseBody
    public CommonResult updateBatch(@RequestBody List<Integer> mids){
        try{
            if(mids == null){
                return CommonResult.fail(500,"更新失败");
            }
            Integer list = goodsService.UpdateGoodsStateBatch(mids);
            return CommonResult.success(null,list,null);
        }catch (RuntimeException e ){
            e.printStackTrace();
            return CommonResult.fail(500,"更新失败");
        }
    }

    @GetMapping("/goodInfo")
    @ResponseBody
    public CommonResult getGoods(){
        return CommonResult.success(goodsService.getGoodsList(),null,null);
    }
}
